/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/5/11
 * \* Time: 21:06
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 把字符串转换成整数 {
    public int strToInt(String str) {
        char[] c=str.trim().toCharArray();
        if(c.length==0){
            return 0;
        }
        int res = 0, bndry = Integer.MAX_VALUE / 10;
        int i=1,sign=1;
        if(c[0]=='-'){
            sign=-1;
        }else if(c[0]!='+'){
            i=0;
        }
        for(int j=i;j<c.length;j++){
            //碰到非数字则终止
            if(c[j] < '0' || c[j] > '9') {
                break;
            }
            //判断是否越界
            if(res > bndry || res == bndry && c[j] > '7') {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            res = res * 10 + (c[j] - '0');
        }
        return sign * res;
    }
}